import datetime

from pCommon import Configuration
from pCommon import Defaults

from pCentralServer import Program

class MachineManaging( object ):

    def __init__( self ):
        print "MachineManaging initializing"

        self._machineList = {}
        self._intervalMethods = self._intervalMethods or []

        csUpdateInterval = float(Program.Program.configStatic.centralserver.updateinterval)

        try:
            mmUpdateInterval = float(Program.Program.configStatic.centralserver.mmUpdateInterval)
        except Configuration.OptionError:
            mmUpdateInterval = Program.Program.configStatic.addValue("centralserver", "mmUpdateInterval", Defaults.mmUpdateInterval)
            Program.Program.configStatic.saveConfig()

        self._intervalMethods.append( [mmUpdateInterval//csUpdateInterval, 0,  self.__manageMachines] )

    def registerMachine( self, machine ):
        """
        """

        self._machineList[tuple( machine )] = datetime.datetime.now() + datetime.timedelta( seconds = Defaults.mmUpdateInterval * 2 )

        return True

    def retrieveMachines( self ):
        """
        """

        return self._machineList.keys()

    def __manageMachines( self ):
        """
        """

        now = datetime.datetime.now()

        self._machineList = {k: v for k, v in self._machineList.iteritems() if v > now}
